package com.zlt.bkparking.controller.salesman;

import com.github.pagehelper.PageInfo;
import com.zlt.bkparking.dto.EquipmentDto;
import com.zlt.bkparking.dto.QueryParams;
import com.zlt.bkparking.entity.Equipment;
import com.zlt.bkparking.service.EquipmentService;
import com.zlt.bkparking.service.ParkingService;
import com.zlt.bkparking.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Author: hzj
 * @Description:
 */

@RestController
@RequestMapping("/equipment")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private ParkingService parkingService;



    @GetMapping("list")
    public Result list(){
        List<Equipment> list = equipmentService.list();
        return Result.ok("查询成功",list);
    }

    @PostMapping("edit")
    public Result edit(@RequestBody EquipmentDto equipment){
        return equipmentService.update(equipment)?Result.ok("更新成功"):Result.error("更新失败");
    }

    @PostMapping("save")
    public Result save(@RequestBody EquipmentDto equipment){
        return  equipmentService.save(equipment)?Result.ok("新增成功"):Result.error("新增失败");
    }

    @PostMapping("del/{id}")
    public Result del(@PathVariable("id") Integer id){
        return equipmentService.remove(id) ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @GetMapping("get")
    @CrossOrigin
    public Result getById(@RequestParam("id") Integer id){
        Equipment equipment = equipmentService.getById(id);
        return Result.ok("查询成功",equipment);
    }

    @PostMapping("page")
    public Result page(@RequestBody QueryParams params){
        PageInfo<Equipment> page = equipmentService.page(params);
        List<HashMap<String, Object>> parking = parkingService.getSelectParking();
        HashMap<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("parking",parking);
        return Result.ok("查询成功",map);
    }

}
